package com.xiaochong.tir.manager.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Navigation;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.NavigationService;
import com.xiaochong.tir.manager.util.ManagerVersion;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘晓阳
 * @create 2017-12-28 20:35
 **/
@Controller
@RequestMapping(ManagerVersion.API_V1_PREFIX+"/navigation/rest")
@Api("菜单管理")
public class NavigationRestController {

    @Autowired
    private NavigationService navigationService;
    @Autowired
    private RedisServer redisServer;
    /**
     * 目录列表
     *
     * @return
     */
    @ApiOperation(value = "查询菜单列表",notes = "查询菜单列表")
    @PostMapping(value = "/restList")
    @ResponseBody
    public Object resListJson(@RequestParam("page") int page,@RequestParam("limit") int limit){
        try {
        	PageInfo<Navigation> pageInfo =  navigationService.getNavigationByParent(page, limit);
        	return new ReturnResult<Navigation>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
            LogTrace.error(getClass().getSimpleName()+"/restList", e.getMessage(), e);
        }
        return null;
    }

    /**
     * 菜单列表
     *
     * @return
     */
    @ApiOperation(value = "查询子菜单列表",notes = "查询子菜单列表")
    @PostMapping(value = "/restChildList")
    @ResponseBody
    public Object restChildListJson(@RequestParam("parentId") Integer parentId,
    		@RequestParam("page") int page,@RequestParam("limit") int limit){
        try {
            PageHelper.startPage(page, limit);
            PageInfo<Navigation> pageInfo = new PageInfo<Navigation>(navigationService.getNavigationByParentId(parentId));
            return new ReturnResult<Navigation>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
        	 LogTrace.error(getClass().getSimpleName()+"/restChildList", e.getMessage(), e);
        }
        return null;
    }

    /**
     * 编辑目录菜单
     *
     * @return
     */
    @ApiOperation(value = "编辑菜单",notes = "编辑菜单")
    @RequestMapping(value = "/editNavigation", method = RequestMethod.POST)
    @ResponseBody
    public int editNavigation(@RequestParam("id") Integer id,
                               @RequestParam("title") String title,
                               @RequestParam("icon") String icon,
                               @RequestParam("href") String href,
                               @RequestParam("rank") Integer rank){
        redisServer.set(RedisKeysUtils.LOGINNAME,"编辑菜单");
        try {
            Navigation nv = navigationService.selectByPrimaryKey(id);
            Navigation navigation = new Navigation();
            navigation.setId(id);
            navigation.setTitle(title);
            navigation.setIcon(icon);
            navigation.setHref(href);
            navigation.setRank(rank);
            navigation.setStatus(nv.getStatus());
            int updateNum = navigationService.updateByPrimaryKey(navigation,nv);
            return updateNum;
        } catch (Exception e) {
        	LogTrace.error(getClass().getSimpleName()+"/editNavigation", e.getMessage(), e);
            return -1;
        }
    }

    /**
     * 删除目录菜单
     *
     * @return
     */
    @ApiOperation(value = "删除菜单",notes = "删除菜单")
    @GetMapping("/deleteNavById")
    @ResponseBody
    public ResultEntity<JSONObject> deleteNavById(@RequestParam("id") Integer id){
        redisServer.set(RedisKeysUtils.LOGINNAME,"删除菜单");
        ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
            navigationService.deleteNavById(id,navigationService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
            //LogTrace.info(getClass(),"[back/auth/navigation/rest/deleteNavById]:"+result.toJSONString());
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName()+"/deleteNavById", e.getMessage(), e);
        }
        return result;
    }

    /**
     * 删除子菜单
     *
     * @return
     */
    @ApiOperation(value = "删除菜单",notes = "删除菜单")
    @GetMapping("/deleteNavByParentId")
    @ResponseBody
    public ResultEntity<JSONObject> deleteNavByParentId(@RequestParam("id") Integer id){
        redisServer.set(RedisKeysUtils.LOGINNAME,"删除菜单");
        ResultEntity<JSONObject> result = new ResultEntity<>();
        List<Integer> navigationIds = new ArrayList<>();
        try {
            List<Navigation> navigations = navigationService.getNavigationByParentId(id);
            for (Navigation navigation: navigations) {
                navigationIds.add(navigation.getId());
                List<Navigation> childNavigations = navigationService.getNavigationByParentId(navigation.getId());
                for(Navigation nav: childNavigations){
                    navigationIds.add(nav.getId());
                }
            }
            navigationIds.add(id);
            for(Integer navId: navigationIds){
                Navigation old = navigationService.selectByPrimaryKey(navId);
                navigationService.deleteNavByParentId(navId,old);
            }
            result.setStatus(StatusEnum.OK);
           // LogTrace.info(getClass(),"[back/auth/navigation/rest/deleteNavByParentId]");
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName()+"/deleteNavByParentId", e.getMessage(), e);
        }
        return result;
    }

}
